#include "ChunkCacheProvider.h"
#include "RenderListBase.h"

void ChunkCacheProvider::Load(const int &index, RenderListBase *list)
{
	FILE *fp = fopen((_str << index).c_str().AsChar(), "rb");

	if (fp == nullptr)
		return;

	size_t size;
	fread(&size, sizeof(size), 1, fp);
	list->Faces.resize(size);
	fread(list->Faces.data(), sizeof(Face), size, fp);

	fread(&size, sizeof(size), 1, fp);
	list->Boxes.resize(size);
	fread(list->Boxes.data(), sizeof(RenderBox), size, fp);

	fclose(fp);
}

void ChunkCacheProvider::Save(const int &index, const RenderListBase *list)
{
	FILE *fp = fopen((_str << index).c_str().AsChar(), "wb+");

	auto size = list->Faces.size();
	fwrite(&size, sizeof(size), 1, fp);
	fwrite(list->Faces.data(), sizeof(Face), size, fp);

	size = list->Boxes.size();
	fwrite(&size, sizeof(size), 1, fp);
	fwrite(list->Boxes.data(), sizeof(RenderBox), size, fp);

	fclose(fp);
}